﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Backend.DTO;
using Backend.BLL;

namespace ReSeSSApplication
{
    public partial class NewStudy : System.Web.UI.Page
    {
        protected UserDTO loggedInUserDTO = new UserDTO();
        StudyBLL aStudyBLL = new StudyBLL();
        ListBLL aListBLL = new ListBLL();


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                AlertError.Visible = false;
                AlertSuccess.Visible = false;
                AlertInfo.Visible = false;

                loggedInUserDTO = (UserDTO)Session["LoggedInUserDTO"];
                RedirectUser(loggedInUserDTO);

                if (!IsPostBack)
                {
                    if (Request.QueryString["update"] == "y")
                    {
                        Session["StudyId"] = null;
                        Session["EditStudy"] = null;

                        AlertSuccess.InnerText = "Study successfully saved and archived in Previous Studies";
                        AlertSuccess.Visible = true;
                    }

                    StudyDTO aStudyDto = null;

                    if (Session["StudyId"] != null)
                    {
                        int studyId = Convert.ToInt32(Session["StudyId"]);
                        aStudyDto = aStudyBLL.GetStudy(studyId);
                    }
                    else
                    {
                        aStudyDto = aStudyBLL.GetUnsavedStudyByUserId(loggedInUserDTO.Id);
                    }

                    if (aStudyDto == null)
                    {
                        aStudyDto = new StudyDTO();
                        aStudyDto.Name = string.Empty;
                        aStudyDto.CreatedBy = loggedInUserDTO.Id;
                        aStudyDto.SharedBy = 0;
                        aStudyDto.SharedMessage = string.Empty;
                        aStudyDto.IsSaved = false;
                        aStudyDto.CreationDate = DateTime.Now;
                        aStudyDto.ModifiedDate = DateTime.Now;

                        int studyId = aStudyBLL.AddStudy(aStudyDto);
                        if (studyId > 0)
                        {
                            Session["StudyId"] = studyId.ToString();
                            StudyIdHiddenField.Value = studyId.ToString();
                            StudyNameHiddenField.Value = string.Empty;
                        }
                    }
                    else
                    {
                        Session["StudyId"] = aStudyDto.Id.ToString();
                        StudyIdHiddenField.Value = aStudyDto.Id.ToString();
                        StudyNameHiddenField.Value = aStudyDto.Name.ToString();
                    }

                    ControlMode(aStudyDto);
                }
                else
                {
                    if (Session["StudyId"] == null)
                    {
                        Response.Redirect("UserDashboard.aspx");
                    }
                }


                /* Inclusion ListBox double clicked checking */
                if (!string.IsNullOrEmpty(InclusionHiddenField.Value))
                {
                    try
                    {
                        if (InclusionHiddenField.Value == "InclusionListDoubleClicked")
                        {
                            int inclusionId = Convert.ToInt32(lstBoxInclusion.SelectedValue);
                            Session["InclusionOrParameterId"] = inclusionId;
                            Response.Redirect("Outcomes.aspx");
                        }
                    }
                    catch (FormatException)
                    {
                        AlertError.InnerText = "No inclusion selected.";
                        AlertError.Visible = true;
                    }
                    catch (Exception ex)
                    {
                        AlertError.InnerText = ex.Message;
                        AlertError.Visible = true;
                    }
                }

                /* Parameter ListBox double clicked checking */
                if (!string.IsNullOrEmpty(ParameterHiddenField.Value))
                {
                    try
                    {
                        if (ParameterHiddenField.Value == "ParameterListDoubleClicked")
                        {
                            int parameterId = Convert.ToInt32(lstBoxParameter.SelectedValue);
                            Session["InclusionOrParameterId"] = parameterId;
                            Response.Redirect("Outcomes.aspx");
                        }
                    }
                    catch (FormatException)
                    {
                        AlertError.InnerText = "No parameter selected.";
                        AlertError.Visible = true;
                    }
                    catch (Exception ex)
                    {
                        AlertError.InnerText = ex.Message;
                        AlertError.Visible = true;
                    }
                }

                if (!IsPostBack)
                {
                    LoadUserListBox();
                    LoadSharedInclusionGridView();
                    LoadSharedParameterGridView();
                    LoadListbox();
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        private void ControlMode(StudyDTO aStudyDto)
        {
            if (Session["EditStudy"] == null)     /* new mode */
            {
                MenuItem.InnerText = "New Study";
                BreadCrumb.InnerText = "New Study";
                CancelEditButton.Visible = false;
                NewStudyHeading.InnerText = "New Study";
            }
            else     /* edit mode */
            {
                MenuItem.InnerText = "Edit Study";
                BreadCrumb.InnerText = "Edit Study";
                CancelEditButton.Visible = true;
                NewStudyHeading.InnerText = "Editing " + aStudyDto.Name;
            }
        }


        #region Save Study

        protected void SaveStudyButton_Click(object sender, EventArgs e)
        {
            try
            {
                string studyName = StudyNameHiddenField.Value.Trim();
                SaveStudy(studyName);
                Response.Redirect("NewStudy.aspx?update=y");
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void SaveStudyNameButton_Click(object sender, EventArgs e)
        {
            try
            {
                string studyName = txtStudyName.Text.Trim();
                SaveStudy(studyName);
                Response.Redirect("NewStudy.aspx?update=y");
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void CancelEditButton_Click(object sender, EventArgs e)
        {
            try
            {
                Session["StudyId"] = null;
                Session["EditStudy"] = null;
                Response.Redirect("NewStudy.aspx");
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        private void SaveStudy(string studyName)
        {
            int inclusionNumber = lstBoxInclusion.Items.Count;
            int parameterNumber = lstBoxParameter.Items.Count;
            studyName = studyName.Trim();

            if (inclusionNumber <= 0 && parameterNumber <= 0)
            {
                throw new Exception("At least one Inclusion or Parameter is needed to save Study");
            }

            if (string.IsNullOrEmpty(studyName))
            {
                throw new Exception("Study Name cannot be empty");
            }

            try
            {
                int studyId = Convert.ToInt32(StudyIdHiddenField.Value);
                StudyDTO aStudyDto = aStudyBLL.GetStudy(studyId);

                aStudyDto.Name = studyName;
                aStudyDto.IsSaved = true;

                aStudyBLL.UpdateStudy(aStudyDto);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


        #region Inclusion

        protected void AddInclusionButton_Click(object sender, EventArgs e)
        {
            try
            {
                int studyId = Convert.ToInt32(StudyIdHiddenField.Value);
                string name = txtInclusionName.Text.Trim();

                if (!string.IsNullOrEmpty(name))
                {
                    InclusionOrParameterDTO aInclusionDTO = new InclusionOrParameterDTO();
                    aInclusionDTO.StudyId = studyId;
                    aInclusionDTO.Name = name;
                    aInclusionDTO.Type = "Inclusion";
                    aInclusionDTO.CreatedBy = loggedInUserDTO.Id;

                    try
                    {
                        aStudyBLL.AddInclusionOrParameter(aInclusionDTO);
                        AlertSuccess.InnerText = "Inclusion added successfully.";
                        AlertSuccess.Visible = true;

                        LoadListbox();
                        txtInclusionName.Text = string.Empty;
                    }
                    catch (Exception ex)
                    {
                        AlertError.InnerText = ex.Message;
                        AlertError.Visible = true;
                    }
                }
                else
                {
                    AlertError.InnerText = "No Inclusion name provided.";
                    AlertError.Visible = true;
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void EditInclusionLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                int inclusionId = Convert.ToInt32(lstBoxInclusion.SelectedValue);
                Session["InclusionOrParameterId"] = inclusionId;
                Response.Redirect("Outcomes.aspx");
            }
            catch (FormatException)
            {
                AlertError.InnerText = "No inclusion selected.";
                AlertError.Visible = true;
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void DeleteInclusionLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                int inclusionId = Convert.ToInt32(lstBoxInclusion.SelectedValue);
                aStudyBLL.DeleteInclusionOrParameter(inclusionId);
                LoadListbox();

                AlertSuccess.InnerText = "Inclusion deleted successfully.";
                AlertSuccess.Visible = true;
            }
            catch (FormatException)
            {
                AlertError.InnerText = "No inclusion selected.";
                AlertError.Visible = true;
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void IncludeSharedInclusionButton_Click(object sender, EventArgs e)
        {
            try
            {
                int checkedInclusion = 0;
                int studyId = Convert.ToInt32(StudyIdHiddenField.Value);

                foreach (GridViewRow row in SharedInclusionGridView.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("SelectInclusionCheckBox");
                    
                    if (cb != null && cb.Checked)
                    {
                        checkedInclusion++;

                        HiddenField inclusionIdHiddenField = (HiddenField)row.FindControl("InclusionIdHiddenField");
                        int inclusionId = Convert.ToInt32(inclusionIdHiddenField.Value);

                        InclusionOrParameterDTO sharedInclusion = aStudyBLL.GetInclusionOrParameter(inclusionId);
                        sharedInclusion.StudyId = studyId;
                        aStudyBLL.UpdateInclusionOrParameter(sharedInclusion);
                    }
                }

                if (checkedInclusion > 0)
                {
                    LoadSharedInclusionGridView();
                    LoadListbox();

                    AlertSuccess.InnerText = "Inclusion(s) included with the study successfully.";
                    AlertSuccess.Visible = true;
                }
                else
                {
                    AlertError.InnerText = "No Inclusion selected to be included with the study.";
                    AlertError.Visible = true;
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        #endregion


        #region Parameter

        protected void AddParameterButton_Click(object sender, EventArgs e)
        {
            try
            {
                int studyId = Convert.ToInt32(StudyIdHiddenField.Value);
                string parameterName = txtParameterName.Text.Trim();

                if (!string.IsNullOrEmpty(parameterName))
                {
                    InclusionOrParameterDTO aParameterDTO = new InclusionOrParameterDTO();
                    aParameterDTO.StudyId = studyId;
                    aParameterDTO.Name = parameterName;
                    aParameterDTO.Type = "Parameter";
                    aParameterDTO.CreatedBy = loggedInUserDTO.Id;

                    try
                    {
                        aStudyBLL.AddInclusionOrParameter(aParameterDTO);
                        AlertSuccess.InnerText = "Parameter added successfully.";
                        AlertSuccess.Visible = true;

                        LoadListbox();
                        txtParameterName.Text = string.Empty;
                    }
                    catch (Exception ex)
                    {
                        AlertError.InnerText = ex.Message;
                        AlertError.Visible = true;
                    }
                }
                else
                {
                    AlertError.InnerText = "No Inclusion name provided.";
                    AlertError.Visible = true;
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void EditParameterLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                int parameterId = Convert.ToInt32(lstBoxParameter.SelectedValue);
                Session["InclusionOrParameterId"] = parameterId;
                Response.Redirect("Outcomes.aspx");
            }
            catch (FormatException)
            {
                AlertError.InnerText = "No parameter selected.";
                AlertError.Visible = true;
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void DeleteParameterLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                int parameterId = Convert.ToInt32(lstBoxParameter.SelectedValue);
                aStudyBLL.DeleteInclusionOrParameter(parameterId);
                LoadListbox();

                AlertSuccess.InnerText = "Parameter deleted successfully.";
                AlertSuccess.Visible = true;
            }
            catch (FormatException)
            {
                AlertError.InnerText = "No parameter selected.";
                AlertError.Visible = true;
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void IncludeSharedParameterButton_Click(object sender, EventArgs e)
        {
            try
            {
                int checkedParameter = 0;
                int studyId = Convert.ToInt32(StudyIdHiddenField.Value);

                foreach (GridViewRow row in SharedParameterGridView.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("SelectParameterCheckBox");

                    if (cb != null && cb.Checked)
                    {
                        checkedParameter++;

                        HiddenField parameterIdHiddenField = (HiddenField)row.FindControl("ParameterIdHiddenField");
                        int parameterId = Convert.ToInt32(parameterIdHiddenField.Value);

                        InclusionOrParameterDTO sharedParameter = aStudyBLL.GetInclusionOrParameter(parameterId);
                        sharedParameter.StudyId = studyId;
                        aStudyBLL.UpdateInclusionOrParameter(sharedParameter);
                    }
                }

                if (checkedParameter > 0)
                {
                    LoadSharedInclusionGridView();
                    LoadListbox();

                    AlertSuccess.InnerText = "Inclusion(s) included with the study successfully.";
                    AlertSuccess.Visible = true;
                }
                else
                {
                    AlertError.InnerText = "No Inclusion selected to be included with the study.";
                    AlertError.Visible = true;
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        #endregion


        protected void ShareButton_Click(object sender, EventArgs e)
        {
            try
            {
                int numberOfUsers = 0;
                string propertyType = PropertyTypeHiddenField.Value;
                int inclusionOrParameterId = Convert.ToInt32(InclusionOrParameterIdHiddenField.Value);

                foreach (ListItem item in userListBox.Items)
                {
                    if (item.Selected)
                    {
                        numberOfUsers++;
                        aStudyBLL.ShareInclusionOrParameter(inclusionOrParameterId, loggedInUserDTO.Id, Convert.ToInt32(item.Value));
                    }
                }

                if (numberOfUsers > 0)
                {
                    AlertSuccess.InnerText = propertyType + " shared successfully.";
                    AlertSuccess.Visible = true;
                }
                else
                {
                    AlertError.InnerText = "No User selected to share the " + propertyType;
                    AlertError.Visible = true;
                }
            }
            catch (Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }


        #region Load Methods

        private void LoadSharedInclusionGridView()
        {
            SharedInclusionGridView.DataSource = aStudyBLL.GetSharedInclusionsByUserId(loggedInUserDTO.Id);
            SharedInclusionGridView.DataBind();
        }

        private void LoadSharedParameterGridView()
        {
            SharedParameterGridView.DataSource = aStudyBLL.GetSharedParametersByUserId(loggedInUserDTO.Id);
            SharedParameterGridView.DataBind();
        }

        private void LoadUserListBox()
        {
            userListBox.DataSource = new UserBLL().GetAllUser("User", loggedInUserDTO.Id);
            userListBox.DataTextField = "LoginId";
            userListBox.DataValueField = "Id";
            userListBox.DataBind();
        }

        private void LoadListbox()
        {
            try
            {
                int studyId = Convert.ToInt32(StudyIdHiddenField.Value);

                if (studyId > 0)
                {
                    List<InclusionOrParameterDTO> listOfInclusions = aStudyBLL.GetInclusionsByStudyId(studyId);
                    lstBoxInclusion.DataSource = listOfInclusions;
                    lstBoxInclusion.DataTextField = "Name";
                    lstBoxInclusion.DataValueField = "Id";
                    lstBoxInclusion.DataBind();

                    List<InclusionOrParameterDTO> listOfParameters = aStudyBLL.GetParametersByStudyId(studyId);
                    lstBoxParameter.DataSource = listOfParameters;
                    lstBoxParameter.DataTextField = "Name";
                    lstBoxParameter.DataValueField = "Id";
                    lstBoxParameter.DataBind();

                    // crate popovers for inclusion list items
                    for (int i = 0; i < lstBoxInclusion.Items.Count; i++)
                    {
                        string outcomeText = string.Empty;
                        int innclusionOrParameterId = Convert.ToInt32(lstBoxInclusion.Items[i].Value);
                        List<OutcomeDTO> listOfOutomes = aStudyBLL.GetOutcomesByInclusionOrParameterId(innclusionOrParameterId);

                        for (int j = 0; j < listOfOutomes.Count; j++)
                        {
                            if (j == 3) { break; }

                            OutcomeDetailsDTO aOutcomeDetailsDTO = aStudyBLL.GetOutcomeDetails(listOfOutomes[j].Id);


                            // Building codeString --------------------------------------------------------------------

                            string codeString = string.Empty;
                            string codeType = string.Empty;
                            if (aOutcomeDetailsDTO.CodeType == 1) { codeType = "ICD10"; }
                            else if (aOutcomeDetailsDTO.CodeType == 2) { codeType = "ICD9"; }
                            else if (aOutcomeDetailsDTO.CodeType == 3) { codeType = "ICD8"; }
                            else if (aOutcomeDetailsDTO.CodeType == 4) { codeType = "AGK"; }

                            int codeListType = 0;
                            if (aOutcomeDetailsDTO.CodeType == 1) { codeListType = 1; }
                            else if (aOutcomeDetailsDTO.CodeType == 2) { codeListType = 2; }
                            else if (aOutcomeDetailsDTO.CodeType == 3) { codeListType = 3; }
                            else if (aOutcomeDetailsDTO.CodeType == 4) { codeListType = 4; }

                            GenericListDTO codeList = aListBLL.GetGenericList(aOutcomeDetailsDTO.CodeId, codeListType);
                            string codeDesription = string.Empty;
                            if (codeList != null) { codeDesription = codeList.Description; }

                            if (!string.IsNullOrEmpty(codeDesription))
                            {
                                codeString += "<tr>";
                                codeString += "<th>Code</th>";
                                codeString += "<td>" + codeType + "</td>";
                                codeString += "<td>" + codeDesription + "</td>";
                                codeString += "</tr>";
                            }


                            // Building codeRestrictString ------------------------------------------------------------

                            string codeRestrictString = string.Empty;
                            string codeRestrictType = string.Empty;
                            if (aOutcomeDetailsDTO.CodeRestrictType == 1) { codeRestrictType = "Related To"; }
                            else if (aOutcomeDetailsDTO.CodeRestrictType == 2) { codeRestrictType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.CodeRestrictType == 3) { codeRestrictType = "Restrict by"; }

                            string codeRestrictField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.CodeRestrictField1))
                                {
                                    if (aOutcomeDetailsDTO.CodeRestrictField1.Length == 10)
                                    {
                                        codeRestrictField1 = aOutcomeDetailsDTO.CodeRestrictField1.ToString();
                                    }
                                    else
                                    {
                                        codeRestrictField1 = Convert.ToInt32(aOutcomeDetailsDTO.CodeRestrictField1).ToString();

                                        if (codeRestrictField1 == "1") { codeRestrictField1 = "Admission Date"; }
                                        else if (codeRestrictField1 == "2") { codeRestrictField1 = "Discharge Date"; }
                                        else if (codeRestrictField1 == "3") { codeRestrictField1 = "Visiting Date"; }
                                        else if (codeRestrictField1 == "4") { codeRestrictField1 = "Date of Birth"; }
                                        else if (codeRestrictField1 == "5") { codeRestrictField1 = "Date of Death"; }
                                        else if (codeRestrictField1 == "6") { codeRestrictField1 = "Parameter"; }
                                        else { codeRestrictField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string codeRestrictField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.CodeRestrictField2))
                                {
                                    if (aOutcomeDetailsDTO.CodeRestrictField2.Length == 10)
                                    {
                                        codeRestrictField2 = aOutcomeDetailsDTO.CodeRestrictField2.ToString();
                                    }
                                    else
                                    {
                                        codeRestrictField2 = Convert.ToInt32(aOutcomeDetailsDTO.CodeRestrictField2).ToString();

                                        if (codeRestrictField2 == "1") { codeRestrictField2 = "Admission Date"; }
                                        else if (codeRestrictField2 == "2") { codeRestrictField2 = "Discharge Date"; }
                                        else if (codeRestrictField2 == "3") { codeRestrictField2 = "Visiting Date"; }
                                        else if (codeRestrictField2 == "4") { codeRestrictField2 = "Date of Birth"; }
                                        else if (codeRestrictField2 == "5") { codeRestrictField2 = "Date of Death"; }
                                        else if (codeRestrictField2 == "6") { codeRestrictField2 = "Parameter"; }
                                        else { codeRestrictField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(codeRestrictField1) || !string.IsNullOrEmpty(codeRestrictField2))
                            {
                                if (!string.IsNullOrEmpty(codeRestrictField1))
                                {
                                    codeRestrictField2 = "<br />" + codeRestrictField2;
                                }

                                codeRestrictString += "<tr>";
                                codeRestrictString += "<th>Code Restrict</th>";
                                codeRestrictString += "<td>" + codeRestrictType + "</td>";
                                codeRestrictString += "<td>" + codeRestrictField1 + codeRestrictField2 + "</td>";
                                codeRestrictString += "</tr>";
                            }
                            

                            // Building dateString --------------------------------------------------------------------

                            string dateString = string.Empty;
                            string dateTimeType = string.Empty;
                            if (aOutcomeDetailsDTO.DateType == 1) { dateTimeType = "Date/Time"; }
                            else if (aOutcomeDetailsDTO.DateType == 2) { dateTimeType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.DateType == 3) { dateTimeType = "Restrict by"; }

                            string dateTimeField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.DateField1))
                                {
                                    if (aOutcomeDetailsDTO.DateField1.Length == 10)
                                    {
                                        dateTimeField1 = aOutcomeDetailsDTO.DateField1.ToString();
                                    }
                                    else
                                    {
                                        dateTimeField1 = Convert.ToInt32(aOutcomeDetailsDTO.DateField1).ToString();

                                        if (dateTimeField1 == "1") { dateTimeField1 = "Admission Date"; }
                                        else if (dateTimeField1 == "2") { dateTimeField1 = "Discharge Date"; }
                                        else if (dateTimeField1 == "3") { dateTimeField1 = "Visiting Date"; }
                                        else if (dateTimeField1 == "4") { dateTimeField1 = "Date of Birth"; }
                                        else if (dateTimeField1 == "5") { dateTimeField1 = "Date of Death"; }
                                        else if (dateTimeField1 == "6") { dateTimeField1 = "Parameter"; }
                                        else { dateTimeField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string dateTimeField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.DateField2))
                                {
                                    if (aOutcomeDetailsDTO.DateField2.Length == 10)
                                    {
                                        dateTimeField2 = aOutcomeDetailsDTO.DateField2.ToString();
                                    }
                                    else
                                    {
                                        dateTimeField2 = Convert.ToInt32(aOutcomeDetailsDTO.DateField2).ToString();

                                        if (dateTimeField2 == "1") { dateTimeField2 = "Admission Date"; }
                                        else if (dateTimeField2 == "2") { dateTimeField2 = "Discharge Date"; }
                                        else if (dateTimeField2 == "3") { dateTimeField2 = "Visiting Date"; }
                                        else if (dateTimeField2 == "4") { dateTimeField2 = "Date of Birth"; }
                                        else if (dateTimeField2 == "5") { dateTimeField2 = "Date of Death"; }
                                        else if (dateTimeField2 == "6") { dateTimeField2 = "Parameter"; }
                                        else { dateTimeField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(dateTimeField1) || !string.IsNullOrEmpty(dateTimeField2))
                            {
                                if (!string.IsNullOrEmpty(dateTimeField1))
                                {
                                    dateTimeField2 = "<br />" + dateTimeField2;
                                }

                                dateString += "<tr>";
                                dateString += "<th>Date/Time</th>";
                                dateString += "<td>" + dateTimeType + "</td>";
                                dateString += "<td>" + dateTimeField1 + dateTimeField2 + "</td>";
                                dateString += "</tr>";
                            }


                            // Building readingsString ----------------------------------------------------------------

                            string readingsString = string.Empty;
                            string readingsType = string.Empty;
                            if (aOutcomeDetailsDTO.ReadingsType == 1) { readingsType = "Laboratory"; }
                            else if (aOutcomeDetailsDTO.ReadingsType == 2) { readingsType = "Pharmaceuticals"; }
                            else if (aOutcomeDetailsDTO.ReadingsType == 3) { readingsType = "Measurement"; }

                            string readingsDescription = string.Empty;
                            if (aOutcomeDetailsDTO.ReadingsType == 2)
                            {
                                PharmaceuticalsListsDTO readingsList = aListBLL.GetPharmaceuticalsListsById(aOutcomeDetailsDTO.ReadingsId);
                                if (readingsList != null) { readingsDescription = readingsList.Name; }
                            }
                            else if (aOutcomeDetailsDTO.ReadingsType == 1 || aOutcomeDetailsDTO.ReadingsType == 3)
                            {
                                int readingsListType = 0;
                                if (aOutcomeDetailsDTO.ReadingsType == 1) { readingsListType = 5; }
                                else if (aOutcomeDetailsDTO.ReadingsType == 3) { readingsListType = 6; }

                                GenericListDTO readingsList = aListBLL.GetGenericList(aOutcomeDetailsDTO.ReadingsId, readingsListType);
                                if (readingsList != null) { readingsDescription = readingsList.Description; }
                            }

                            if (!string.IsNullOrEmpty(readingsDescription))
                            {
                                readingsString += "<tr>";
                                readingsString += "<th>Readings</th>";
                                readingsString += "<td>" + readingsType + "</td>";
                                readingsString += "<td>" + readingsDescription + "</td>";
                                readingsString += "</tr>";
                            }


                            // Building readingsRestrictString ------------------------------------------------------------

                            string readingsRestrictString = string.Empty;
                            string readingsRestrictType = string.Empty;
                            if (aOutcomeDetailsDTO.ReadingsRestrictType == 1) { readingsRestrictType = "Related To"; }
                            else if (aOutcomeDetailsDTO.ReadingsRestrictType == 2) { readingsRestrictType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.ReadingsRestrictType == 3) { readingsRestrictType = "Restrict by"; }

                            string readingsRestrictField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.ReadingsRestrictField1))
                                {
                                    if (aOutcomeDetailsDTO.ReadingsRestrictField1.Length == 10)
                                    {
                                        readingsRestrictField1 = aOutcomeDetailsDTO.ReadingsRestrictField1.ToString();
                                    }
                                    else
                                    {
                                        readingsRestrictField1 = Convert.ToInt32(aOutcomeDetailsDTO.ReadingsRestrictField1).ToString();

                                        if (readingsRestrictField1 == "1") { readingsRestrictField1 = "Admission Date"; }
                                        else if (readingsRestrictField1 == "2") { readingsRestrictField1 = "Discharge Date"; }
                                        else if (readingsRestrictField1 == "3") { readingsRestrictField1 = "Visiting Date"; }
                                        else if (readingsRestrictField1 == "4") { readingsRestrictField1 = "Date of Birth"; }
                                        else if (readingsRestrictField1 == "5") { readingsRestrictField1 = "Date of Death"; }
                                        else if (readingsRestrictField1 == "6") { readingsRestrictField1 = "Parameter"; }
                                        else { readingsRestrictField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string readingsRestrictField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.ReadingsRestrictField2))
                                {
                                    if (aOutcomeDetailsDTO.ReadingsRestrictField2.Length == 10)
                                    {
                                        readingsRestrictField2 = aOutcomeDetailsDTO.ReadingsRestrictField2.ToString();
                                    }
                                    else
                                    {
                                        readingsRestrictField2 = Convert.ToInt32(aOutcomeDetailsDTO.ReadingsRestrictField2).ToString();

                                        if (readingsRestrictField2 == "1") { readingsRestrictField2 = "Admission Date"; }
                                        else if (readingsRestrictField2 == "2") { readingsRestrictField2 = "Discharge Date"; }
                                        else if (readingsRestrictField2 == "3") { readingsRestrictField2 = "Visiting Date"; }
                                        else if (readingsRestrictField2 == "4") { readingsRestrictField2 = "Date of Birth"; }
                                        else if (readingsRestrictField2 == "5") { readingsRestrictField2 = "Date of Death"; }
                                        else if (readingsRestrictField2 == "6") { readingsRestrictField2 = "Parameter"; }
                                        else { readingsRestrictField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(readingsRestrictField1) || !string.IsNullOrEmpty(readingsRestrictField2))
                            {
                                if (!string.IsNullOrEmpty(readingsRestrictField1))
                                {
                                    readingsRestrictField2 = "<br />" + readingsRestrictField2;
                                }

                                readingsRestrictString += "<tr>";
                                readingsRestrictString += "<th>Readings Restrict</th>";
                                readingsRestrictString += "<td>" + readingsRestrictType + "</td>";
                                readingsRestrictString += "<td>" + readingsRestrictField1 + readingsRestrictField2 + "</td>";
                                readingsRestrictString += "</tr>";
                            }


                            // Building textString --------------------------------------------------------------------

                            string textString = string.Empty;
                            string textType = string.Empty;
                            if (aOutcomeDetailsDTO.TextType == 1) { textType = "Referrals"; }
                            else if (aOutcomeDetailsDTO.TextType == 2) { textType = "Medical File Text"; }

                            string textValue = string.Empty;
                            string textSep1 = string.Empty;
                            string textSep2 = string.Empty;

                            if (aOutcomeDetailsDTO.TextType == 1)
                            {
                                if (aOutcomeDetailsDTO.TextField1 == "1") { textValue += "Profession - Microbiology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "2") { textValue += "Profession - Pharmacology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "3") { textValue += "Profession - Transfusion"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "4") { textValue += "Profession - Cytology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "5") { textValue += "Profession - Neurophysiology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "6") { textValue += "Profession - Radiology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "7") { textValue += "Profession - Physiology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "8") { textValue += "Profession - Immunology"; textSep1 = "<br />"; }    
                            }
                            else
                            {
                                if (aOutcomeDetailsDTO.TextField1 == "1") { textValue += "Profession - Doctor"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "2") { textValue += "Profession - Nurse"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "3") { textValue += "Profession - Assistance"; textSep1 = "<br />"; }
                            }

                            textValue += textSep1;

                            if (aOutcomeDetailsDTO.TextType == 1)
                            {
                                if (aOutcomeDetailsDTO.TextField2 == "1") { textValue += "Type - Emergent"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "2") { textValue += "Type - Non-emergent"; textSep2 = "<br />"; }
                            }
                            else
                            {
                                if (aOutcomeDetailsDTO.TextField2 == "1") { textValue += "Type - Emergency Note"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "2") { textValue += "Type - Admission Note"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "3") { textValue += "Type - Daily Note"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "4") { textValue += "Type - Discharge Note"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "5") { textValue += "Type - Surgical Story"; textSep2 = "<br />"; }
                            }

                            textValue += textSep2;

                            if (aOutcomeDetailsDTO.TextField3 == "1") { textValue += "Department - Department of Anesthesiology, Operations and Intensive Care"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "2") { textValue += "Department - Child"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "3") { textValue += "Department - Breast and Endokrinkirurgiska clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "4") { textValue += "Department - CAST"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "5") { textValue += "Department - ECMO center"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "6") { textValue += "Department - Endokrinmedicinska clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "7") { textValue += "Department - Gastro Center Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "8") { textValue += "Department - Gastro Center Medicine"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "9") { textValue += "Department - Geriatric Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "10") { textValue += "Department - Hematology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "11") { textValue += "Department - Department of Cardiology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "12") { textValue += "Department - Department of Dermatology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "13") { textValue += "Department - Hyperbarmedicin - Pressure vessels"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "14") { textValue += "Department - Hearing and Balance Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "15") { textValue += "Department - Immunodeficiency Unit"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "16") { textValue += "Department - Department of Infectious Diseases"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "17") { textValue += "Department - Obstetrics and Gynecology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "18") { textValue += "Department - Vascular Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "19") { textValue += "Department - Speech Therapy Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "20") { textValue += "Department - Pulmonary-Allergy Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "21") { textValue += "Department - Department of Neurosurgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "22") { textValue += "Department - Department of Neurology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "23") { textValue += "Department - Renal Medicine"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "24") { textValue += "Department - Department of Oncology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "25") { textValue += "Department - Orthopaedic Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "26") { textValue += "Department - Psykologklinien"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "27") { textValue += "Department - Reconstructive plastic surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "28") { textValue += "Department - Rheumatology Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "29") { textValue += "Department - Social Medicine"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "30") { textValue += "Department - Department of Thoracic Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "31") { textValue += "Department - Transplant Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "32") { textValue += "Department - Department of Urology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "33") { textValue += "Department - Ear, Nose and Throat Clinic"; }

                            if (!string.IsNullOrEmpty(textValue))
                            {
                                textString += "<tr>";
                                textString += "<th>Text</th>";
                                textString += "<td>" + textType + "</td>";
                                textString += "<td>" + textValue + "</td>";
                                textString += "</tr>";
                            }


                            // Building textRestrictString ------------------------------------------------------------

                            string textRestrictString = string.Empty;
                            string textRestrictType = string.Empty;
                            if (aOutcomeDetailsDTO.TextRestrictType == 1) { textRestrictType = "Related To"; }
                            else if (aOutcomeDetailsDTO.TextRestrictType == 2) { textRestrictType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.TextRestrictType == 3) { textRestrictType = "Restrict by"; }

                            string textRestrictField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.TextRestrictField1))
                                {
                                    if (aOutcomeDetailsDTO.TextRestrictField1.Length == 10)
                                    {
                                        textRestrictField1 = aOutcomeDetailsDTO.TextRestrictField1.ToString();
                                    }
                                    else
                                    {
                                        textRestrictField1 = Convert.ToInt32(aOutcomeDetailsDTO.TextRestrictField1).ToString();

                                        if (textRestrictField1 == "1") { textRestrictField1 = "Admission Date"; }
                                        else if (textRestrictField1 == "2") { textRestrictField1 = "Discharge Date"; }
                                        else if (textRestrictField1 == "3") { textRestrictField1 = "Visiting Date"; }
                                        else if (textRestrictField1 == "4") { textRestrictField1 = "Date of Birth"; }
                                        else if (textRestrictField1 == "5") { textRestrictField1 = "Date of Death"; }
                                        else if (textRestrictField1 == "6") { textRestrictField1 = "Parameter"; }
                                        else { textRestrictField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string textRestrictField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.TextRestrictField2))
                                {
                                    if (aOutcomeDetailsDTO.TextRestrictField2.Length == 10)
                                    {
                                        textRestrictField2 = aOutcomeDetailsDTO.TextRestrictField2.ToString();
                                    }
                                    else
                                    {
                                        textRestrictField2 = Convert.ToInt32(aOutcomeDetailsDTO.TextRestrictField2).ToString();

                                        if (textRestrictField2 == "1") { textRestrictField2 = "Admission Date"; }
                                        else if (textRestrictField2 == "2") { textRestrictField2 = "Discharge Date"; }
                                        else if (textRestrictField2 == "3") { textRestrictField2 = "Visiting Date"; }
                                        else if (textRestrictField2 == "4") { textRestrictField2 = "Date of Birth"; }
                                        else if (textRestrictField2 == "5") { textRestrictField2 = "Date of Death"; }
                                        else if (textRestrictField2 == "6") { textRestrictField2 = "Parameter"; }
                                        else { textRestrictField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(textRestrictField1) || !string.IsNullOrEmpty(textRestrictField2))
                            {
                                if (!string.IsNullOrEmpty(textRestrictField1))
                                {
                                    textRestrictField2 = "<br />" + textRestrictField2;
                                }

                                textRestrictString += "<tr>";
                                textRestrictString += "<th>Text Restrict</th>";
                                textRestrictString += "<td>" + textRestrictType + "</td>";
                                textRestrictString += "<td>" + textRestrictField1 + textRestrictField2 + "</td>";
                                textRestrictString += "</tr>";
                            }


                            // Building radiologyString --------------------------------------------------------------------

                            string radiologyString = string.Empty;
                            string radiologyType = string.Empty;
                            if (aOutcomeDetailsDTO.RadiologyType == 1) { radiologyType = "MRI"; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 2) { radiologyType = "CT"; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 3) { radiologyType = "X-Ray"; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 4) { radiologyType = "USG"; }

                            int radiologyListType = 0;
                            if (aOutcomeDetailsDTO.RadiologyType == 1) { radiologyListType = 7; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 2) { radiologyListType = 8; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 3) { radiologyListType = 9; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 4) { radiologyListType = 10; }

                            GenericListDTO radiologyList = aListBLL.GetGenericList(aOutcomeDetailsDTO.RadiologyId, radiologyListType);
                            
                            string radiologyDesription = string.Empty;
                            if (radiologyList != null) { radiologyDesription = radiologyList.Description; }

                            if (!string.IsNullOrEmpty(radiologyDesription))
                            {
                                radiologyString += "<tr>";
                                radiologyString += "<th>Radiology</th>";
                                radiologyString += "<td>" + radiologyType + "</td>";
                                radiologyString += "<td>" + radiologyDesription + "</td>";
                                radiologyString += "</tr>";
                            }


                            // Building radiologyRestrictString ------------------------------------------------------------

                            string radiologyRestrictString = string.Empty;
                            string radiologyRestrictType = string.Empty;
                            if (aOutcomeDetailsDTO.RadiologyRestrictType == 1) { radiologyRestrictType = "Related To"; }
                            else if (aOutcomeDetailsDTO.RadiologyRestrictType == 2) { radiologyRestrictType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.RadiologyRestrictType == 3) { radiologyRestrictType = "Restrict by"; }

                            string radiologyRestrictField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.RadiologyRestrictField1))
                                {
                                    if (aOutcomeDetailsDTO.RadiologyRestrictField1.Length == 10)
                                    {
                                        radiologyRestrictField1 = aOutcomeDetailsDTO.RadiologyRestrictField1.ToString();
                                    }
                                    else
                                    {
                                        radiologyRestrictField1 = Convert.ToInt32(aOutcomeDetailsDTO.RadiologyRestrictField1).ToString();

                                        if (radiologyRestrictField1 == "1") { radiologyRestrictField1 = "Admission Date"; }
                                        else if (radiologyRestrictField1 == "2") { radiologyRestrictField1 = "Discharge Date"; }
                                        else if (radiologyRestrictField1 == "3") { radiologyRestrictField1 = "Visiting Date"; }
                                        else if (radiologyRestrictField1 == "4") { radiologyRestrictField1 = "Date of Birth"; }
                                        else if (radiologyRestrictField1 == "5") { radiologyRestrictField1 = "Date of Death"; }
                                        else if (radiologyRestrictField1 == "6") { radiologyRestrictField1 = "Parameter"; }
                                        else { radiologyRestrictField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string radiologyRestrictField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.RadiologyRestrictField2))
                                {
                                    if (aOutcomeDetailsDTO.RadiologyRestrictField2.Length == 10)
                                    {
                                        radiologyRestrictField2 = aOutcomeDetailsDTO.RadiologyRestrictField2.ToString();
                                    }
                                    else
                                    {
                                        radiologyRestrictField2 = Convert.ToInt32(aOutcomeDetailsDTO.RadiologyRestrictField2).ToString();

                                        if (radiologyRestrictField2 == "1") { radiologyRestrictField2 = "Admission Date"; }
                                        else if (radiologyRestrictField2 == "2") { radiologyRestrictField2 = "Discharge Date"; }
                                        else if (radiologyRestrictField2 == "3") { radiologyRestrictField2 = "Visiting Date"; }
                                        else if (radiologyRestrictField2 == "4") { radiologyRestrictField2 = "Date of Birth"; }
                                        else if (radiologyRestrictField2 == "5") { radiologyRestrictField2 = "Date of Death"; }
                                        else if (radiologyRestrictField2 == "6") { radiologyRestrictField2 = "Parameter"; }
                                        else { radiologyRestrictField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(radiologyRestrictField1) || !string.IsNullOrEmpty(radiologyRestrictField2))
                            {
                                if (!string.IsNullOrEmpty(radiologyRestrictField1))
                                {
                                    radiologyRestrictField2 = "<br />" + radiologyRestrictField2;
                                }

                                radiologyRestrictString += "<tr>";
                                radiologyRestrictString += "<th>Radiology Restrict</th>";
                                radiologyRestrictString += "<td>" + radiologyRestrictType + "</td>";
                                radiologyRestrictString += "<td>" + radiologyRestrictField1 + radiologyRestrictField2 + "</td>";
                                radiologyRestrictString += "</tr>";
                            }

                            
                            string outcomeDetailsText = string.Empty;

                            if (aOutcomeDetailsDTO != null)
                            {
                                outcomeDetailsText += "<div class='outcomedetails-popup'>";
                                outcomeDetailsText += "<table class='table table-condensed table-striped'>";
                                    outcomeDetailsText += codeString;
                                    outcomeDetailsText += codeRestrictString;
                                    outcomeDetailsText += dateString;
                                    outcomeDetailsText += readingsString;
                                    outcomeDetailsText += readingsRestrictString;
                                    outcomeDetailsText += textString;
                                    outcomeDetailsText += textRestrictString;
                                    outcomeDetailsText += radiologyString;
                                    outcomeDetailsText += radiologyRestrictString;
                                outcomeDetailsText += "</table>";
                                outcomeDetailsText += "</div>";
                            }

                            outcomeText += "<div>" + listOfOutomes[j].FieldName + ": " + listOfOutomes[j].FieldValue + outcomeDetailsText + "</div>";
                        }

                        if (!string.IsNullOrEmpty(outcomeText))
                        {
                            lstBoxInclusion.Items[i].Attributes.Add("title", lstBoxInclusion.Items[i].Text);
                            lstBoxInclusion.Items[i].Attributes.Add("data-content", outcomeText);
                        }
                    }

                    // crate popovers for parameter list items
                    for (int i = 0; i < lstBoxParameter.Items.Count; i++)
                    {
                        string outcomeText = string.Empty;
                        int innclusionOrParameterId = Convert.ToInt32(lstBoxParameter.Items[i].Value);
                        List<OutcomeDTO> listOfOutomes = aStudyBLL.GetOutcomesByInclusionOrParameterId(innclusionOrParameterId);

                        for (int j = 0; j < listOfOutomes.Count; j++)
                        {
                            if (j == 3) { break; }

                            OutcomeDetailsDTO aOutcomeDetailsDTO = aStudyBLL.GetOutcomeDetails(listOfOutomes[j].Id);


                            // Building codeString --------------------------------------------------------------------

                            string codeString = string.Empty;
                            string codeType = string.Empty;
                            if (aOutcomeDetailsDTO.CodeType == 1) { codeType = "ICD10"; }
                            else if (aOutcomeDetailsDTO.CodeType == 2) { codeType = "ICD9"; }
                            else if (aOutcomeDetailsDTO.CodeType == 3) { codeType = "ICD8"; }
                            else if (aOutcomeDetailsDTO.CodeType == 4) { codeType = "AGK"; }

                            int codeListType = 0;
                            if (aOutcomeDetailsDTO.CodeType == 1) { codeListType = 1; }
                            else if (aOutcomeDetailsDTO.CodeType == 2) { codeListType = 2; }
                            else if (aOutcomeDetailsDTO.CodeType == 3) { codeListType = 3; }
                            else if (aOutcomeDetailsDTO.CodeType == 4) { codeListType = 4; }

                            GenericListDTO codeList = aListBLL.GetGenericList(aOutcomeDetailsDTO.CodeId, codeListType);
                            string codeDesription = string.Empty;
                            if (codeList != null) { codeDesription = codeList.Description; }

                            if (!string.IsNullOrEmpty(codeDesription))
                            {
                                codeString += "<tr>";
                                codeString += "<th>Code</th>";
                                codeString += "<td>" + codeType + "</td>";
                                codeString += "<td>" + codeDesription + "</td>";
                                codeString += "</tr>";
                            }


                            // Building codeRestrictString ------------------------------------------------------------

                            string codeRestrictString = string.Empty;
                            string codeRestrictType = string.Empty;
                            if (aOutcomeDetailsDTO.CodeRestrictType == 1) { codeRestrictType = "Related To"; }
                            else if (aOutcomeDetailsDTO.CodeRestrictType == 2) { codeRestrictType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.CodeRestrictType == 3) { codeRestrictType = "Restrict by"; }

                            string codeRestrictField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.CodeRestrictField1))
                                {
                                    if (aOutcomeDetailsDTO.CodeRestrictField1.Length == 10)
                                    {
                                        codeRestrictField1 = aOutcomeDetailsDTO.CodeRestrictField1.ToString();
                                    }
                                    else
                                    {
                                        codeRestrictField1 = Convert.ToInt32(aOutcomeDetailsDTO.CodeRestrictField1).ToString();

                                        if (codeRestrictField1 == "1") { codeRestrictField1 = "Admission Date"; }
                                        else if (codeRestrictField1 == "2") { codeRestrictField1 = "Discharge Date"; }
                                        else if (codeRestrictField1 == "3") { codeRestrictField1 = "Visiting Date"; }
                                        else if (codeRestrictField1 == "4") { codeRestrictField1 = "Date of Birth"; }
                                        else if (codeRestrictField1 == "5") { codeRestrictField1 = "Date of Death"; }
                                        else if (codeRestrictField1 == "6") { codeRestrictField1 = "Parameter"; }
                                        else { codeRestrictField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string codeRestrictField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.CodeRestrictField2))
                                {
                                    if (aOutcomeDetailsDTO.CodeRestrictField2.Length == 10)
                                    {
                                        codeRestrictField2 = aOutcomeDetailsDTO.CodeRestrictField2.ToString();
                                    }
                                    else
                                    {
                                        codeRestrictField2 = Convert.ToInt32(aOutcomeDetailsDTO.CodeRestrictField2).ToString();

                                        if (codeRestrictField2 == "1") { codeRestrictField2 = "Admission Date"; }
                                        else if (codeRestrictField2 == "2") { codeRestrictField2 = "Discharge Date"; }
                                        else if (codeRestrictField2 == "3") { codeRestrictField2 = "Visiting Date"; }
                                        else if (codeRestrictField2 == "4") { codeRestrictField2 = "Date of Birth"; }
                                        else if (codeRestrictField2 == "5") { codeRestrictField2 = "Date of Death"; }
                                        else if (codeRestrictField2 == "6") { codeRestrictField2 = "Parameter"; }
                                        else { codeRestrictField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(codeRestrictField1) || !string.IsNullOrEmpty(codeRestrictField2))
                            {
                                if (!string.IsNullOrEmpty(codeRestrictField1))
                                {
                                    codeRestrictField2 = "<br />" + codeRestrictField2;
                                }

                                codeRestrictString += "<tr>";
                                codeRestrictString += "<th>Code Restrict</th>";
                                codeRestrictString += "<td>" + codeRestrictType + "</td>";
                                codeRestrictString += "<td>" + codeRestrictField1 + codeRestrictField2 + "</td>";
                                codeRestrictString += "</tr>";
                            }


                            // Building dateString --------------------------------------------------------------------

                            string dateString = string.Empty;
                            string dateTimeType = string.Empty;
                            if (aOutcomeDetailsDTO.DateType == 1) { dateTimeType = "Date/Time"; }
                            else if (aOutcomeDetailsDTO.DateType == 2) { dateTimeType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.DateType == 3) { dateTimeType = "Restrict by"; }

                            string dateTimeField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.DateField1))
                                {
                                    if (aOutcomeDetailsDTO.DateField1.Length == 10)
                                    {
                                        dateTimeField1 = aOutcomeDetailsDTO.DateField1.ToString();
                                    }
                                    else
                                    {
                                        dateTimeField1 = Convert.ToInt32(aOutcomeDetailsDTO.DateField1).ToString();

                                        if (dateTimeField1 == "1") { dateTimeField1 = "Admission Date"; }
                                        else if (dateTimeField1 == "2") { dateTimeField1 = "Discharge Date"; }
                                        else if (dateTimeField1 == "3") { dateTimeField1 = "Visiting Date"; }
                                        else if (dateTimeField1 == "4") { dateTimeField1 = "Date of Birth"; }
                                        else if (dateTimeField1 == "5") { dateTimeField1 = "Date of Death"; }
                                        else if (dateTimeField1 == "6") { dateTimeField1 = "Parameter"; }
                                        else { dateTimeField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string dateTimeField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.DateField2))
                                {
                                    if (aOutcomeDetailsDTO.DateField2.Length == 10)
                                    {
                                        dateTimeField2 = aOutcomeDetailsDTO.DateField2.ToString();
                                    }
                                    else
                                    {
                                        dateTimeField2 = Convert.ToInt32(aOutcomeDetailsDTO.DateField2).ToString();

                                        if (dateTimeField2 == "1") { dateTimeField2 = "Admission Date"; }
                                        else if (dateTimeField2 == "2") { dateTimeField2 = "Discharge Date"; }
                                        else if (dateTimeField2 == "3") { dateTimeField2 = "Visiting Date"; }
                                        else if (dateTimeField2 == "4") { dateTimeField2 = "Date of Birth"; }
                                        else if (dateTimeField2 == "5") { dateTimeField2 = "Date of Death"; }
                                        else if (dateTimeField2 == "6") { dateTimeField2 = "Parameter"; }
                                        else { dateTimeField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(dateTimeField1) || !string.IsNullOrEmpty(dateTimeField2))
                            {
                                if (!string.IsNullOrEmpty(dateTimeField1))
                                {
                                    dateTimeField2 = "<br />" + dateTimeField2;
                                }

                                dateString += "<tr>";
                                dateString += "<th>Date/Time</th>";
                                dateString += "<td>" + dateTimeType + "</td>";
                                dateString += "<td>" + dateTimeField1 + dateTimeField2 + "</td>";
                                dateString += "</tr>";
                            }


                            // Building readingsString ----------------------------------------------------------------

                            string readingsString = string.Empty;
                            string readingsType = string.Empty;
                            if (aOutcomeDetailsDTO.ReadingsType == 1) { readingsType = "Laboratory"; }
                            else if (aOutcomeDetailsDTO.ReadingsType == 2) { readingsType = "Pharmaceuticals"; }
                            else if (aOutcomeDetailsDTO.ReadingsType == 3) { readingsType = "Measurement"; }

                            string readingsDescription = string.Empty;
                            if (aOutcomeDetailsDTO.ReadingsType == 2)
                            {
                                PharmaceuticalsListsDTO readingsList = aListBLL.GetPharmaceuticalsListsById(aOutcomeDetailsDTO.ReadingsId);
                                if (readingsList != null) { readingsDescription = readingsList.Name; }
                            }
                            else if (aOutcomeDetailsDTO.ReadingsType == 1 || aOutcomeDetailsDTO.ReadingsType == 3)
                            {
                                int readingsListType = 0;
                                if (aOutcomeDetailsDTO.ReadingsType == 1) { readingsListType = 5; }
                                else if (aOutcomeDetailsDTO.ReadingsType == 3) { readingsListType = 6; }

                                GenericListDTO readingsList = aListBLL.GetGenericList(aOutcomeDetailsDTO.ReadingsId, readingsListType);
                                if (readingsList != null) { readingsDescription = readingsList.Description; }
                            }

                            if (!string.IsNullOrEmpty(readingsDescription))
                            {
                                readingsString += "<tr>";
                                readingsString += "<th>Readings</th>";
                                readingsString += "<td>" + readingsType + "</td>";
                                readingsString += "<td>" + readingsDescription + "</td>";
                                readingsString += "</tr>";
                            }


                            // Building readingsRestrictString ------------------------------------------------------------

                            string readingsRestrictString = string.Empty;
                            string readingsRestrictType = string.Empty;
                            if (aOutcomeDetailsDTO.ReadingsRestrictType == 1) { readingsRestrictType = "Related To"; }
                            else if (aOutcomeDetailsDTO.ReadingsRestrictType == 2) { readingsRestrictType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.ReadingsRestrictType == 3) { readingsRestrictType = "Restrict by"; }

                            string readingsRestrictField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.ReadingsRestrictField1))
                                {
                                    if (aOutcomeDetailsDTO.ReadingsRestrictField1.Length == 10)
                                    {
                                        readingsRestrictField1 = aOutcomeDetailsDTO.ReadingsRestrictField1.ToString();
                                    }
                                    else
                                    {
                                        readingsRestrictField1 = Convert.ToInt32(aOutcomeDetailsDTO.ReadingsRestrictField1).ToString();

                                        if (readingsRestrictField1 == "1") { readingsRestrictField1 = "Admission Date"; }
                                        else if (readingsRestrictField1 == "2") { readingsRestrictField1 = "Discharge Date"; }
                                        else if (readingsRestrictField1 == "3") { readingsRestrictField1 = "Visiting Date"; }
                                        else if (readingsRestrictField1 == "4") { readingsRestrictField1 = "Date of Birth"; }
                                        else if (readingsRestrictField1 == "5") { readingsRestrictField1 = "Date of Death"; }
                                        else if (readingsRestrictField1 == "6") { readingsRestrictField1 = "Parameter"; }
                                        else { readingsRestrictField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string readingsRestrictField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.ReadingsRestrictField2))
                                {
                                    if (aOutcomeDetailsDTO.ReadingsRestrictField2.Length == 10)
                                    {
                                        readingsRestrictField2 = aOutcomeDetailsDTO.ReadingsRestrictField2.ToString();
                                    }
                                    else
                                    {
                                        readingsRestrictField2 = Convert.ToInt32(aOutcomeDetailsDTO.ReadingsRestrictField2).ToString();

                                        if (readingsRestrictField2 == "1") { readingsRestrictField2 = "Admission Date"; }
                                        else if (readingsRestrictField2 == "2") { readingsRestrictField2 = "Discharge Date"; }
                                        else if (readingsRestrictField2 == "3") { readingsRestrictField2 = "Visiting Date"; }
                                        else if (readingsRestrictField2 == "4") { readingsRestrictField2 = "Date of Birth"; }
                                        else if (readingsRestrictField2 == "5") { readingsRestrictField2 = "Date of Death"; }
                                        else if (readingsRestrictField2 == "6") { readingsRestrictField2 = "Parameter"; }
                                        else { readingsRestrictField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(readingsRestrictField1) || !string.IsNullOrEmpty(readingsRestrictField2))
                            {
                                if (!string.IsNullOrEmpty(readingsRestrictField1))
                                {
                                    readingsRestrictField2 = "<br />" + readingsRestrictField2;
                                }

                                readingsRestrictString += "<tr>";
                                readingsRestrictString += "<th>Readings Restrict</th>";
                                readingsRestrictString += "<td>" + readingsRestrictType + "</td>";
                                readingsRestrictString += "<td>" + readingsRestrictField1 + readingsRestrictField2 + "</td>";
                                readingsRestrictString += "</tr>";
                            }


                            // Building textString --------------------------------------------------------------------

                            string textString = string.Empty;
                            string textType = string.Empty;
                            if (aOutcomeDetailsDTO.TextType == 1) { textType = "Referrals"; }
                            else if (aOutcomeDetailsDTO.TextType == 2) { textType = "Medical File Text"; }

                            string textValue = string.Empty;
                            string textSep1 = string.Empty;
                            string textSep2 = string.Empty;

                            if (aOutcomeDetailsDTO.TextType == 1)
                            {
                                if (aOutcomeDetailsDTO.TextField1 == "1") { textValue += "Profession - Microbiology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "2") { textValue += "Profession - Pharmacology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "3") { textValue += "Profession - Transfusion"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "4") { textValue += "Profession - Cytology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "5") { textValue += "Profession - Neurophysiology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "6") { textValue += "Profession - Radiology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "7") { textValue += "Profession - Physiology"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "8") { textValue += "Profession - Immunology"; textSep1 = "<br />"; }
                            }
                            else
                            {
                                if (aOutcomeDetailsDTO.TextField1 == "1") { textValue += "Profession - Doctor"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "2") { textValue += "Profession - Nurse"; textSep1 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField1 == "3") { textValue += "Profession - Assistance"; textSep1 = "<br />"; }
                            }

                            textValue += textSep1;

                            if (aOutcomeDetailsDTO.TextType == 1)
                            {
                                if (aOutcomeDetailsDTO.TextField2 == "1") { textValue += "Type - Emergent"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "2") { textValue += "Type - Non-emergent"; textSep2 = "<br />"; }
                            }
                            else
                            {
                                if (aOutcomeDetailsDTO.TextField2 == "1") { textValue += "Type - Emergency Note"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "2") { textValue += "Type - Admission Note"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "3") { textValue += "Type - Daily Note"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "4") { textValue += "Type - Discharge Note"; textSep2 = "<br />"; }
                                else if (aOutcomeDetailsDTO.TextField2 == "5") { textValue += "Type - Surgical Story"; textSep2 = "<br />"; }
                            }

                            textValue += textSep2;

                            if (aOutcomeDetailsDTO.TextField3 == "1") { textValue += "Department - Department of Anesthesiology, Operations and Intensive Care"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "2") { textValue += "Department - Child"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "3") { textValue += "Department - Breast and Endokrinkirurgiska clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "4") { textValue += "Department - CAST"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "5") { textValue += "Department - ECMO center"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "6") { textValue += "Department - Endokrinmedicinska clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "7") { textValue += "Department - Gastro Center Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "8") { textValue += "Department - Gastro Center Medicine"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "9") { textValue += "Department - Geriatric Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "10") { textValue += "Department - Hematology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "11") { textValue += "Department - Department of Cardiology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "12") { textValue += "Department - Department of Dermatology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "13") { textValue += "Department - Hyperbarmedicin - Pressure vessels"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "14") { textValue += "Department - Hearing and Balance Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "15") { textValue += "Department - Immunodeficiency Unit"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "16") { textValue += "Department - Department of Infectious Diseases"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "17") { textValue += "Department - Obstetrics and Gynecology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "18") { textValue += "Department - Vascular Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "19") { textValue += "Department - Speech Therapy Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "20") { textValue += "Department - Pulmonary-Allergy Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "21") { textValue += "Department - Department of Neurosurgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "22") { textValue += "Department - Department of Neurology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "23") { textValue += "Department - Renal Medicine"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "24") { textValue += "Department - Department of Oncology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "25") { textValue += "Department - Orthopaedic Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "26") { textValue += "Department - Psykologklinien"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "27") { textValue += "Department - Reconstructive plastic surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "28") { textValue += "Department - Rheumatology Clinic"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "29") { textValue += "Department - Social Medicine"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "30") { textValue += "Department - Department of Thoracic Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "31") { textValue += "Department - Transplant Surgery"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "32") { textValue += "Department - Department of Urology"; }
                            else if (aOutcomeDetailsDTO.TextField3 == "33") { textValue += "Department - Ear, Nose and Throat Clinic"; }

                            if (!string.IsNullOrEmpty(textValue))
                            {
                                textString += "<tr>";
                                textString += "<th>Text</th>";
                                textString += "<td>" + textType + "</td>";
                                textString += "<td>" + textValue + "</td>";
                                textString += "</tr>";
                            }


                            // Building textRestrictString ------------------------------------------------------------

                            string textRestrictString = string.Empty;
                            string textRestrictType = string.Empty;
                            if (aOutcomeDetailsDTO.TextRestrictType == 1) { textRestrictType = "Related To"; }
                            else if (aOutcomeDetailsDTO.TextRestrictType == 2) { textRestrictType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.TextRestrictType == 3) { textRestrictType = "Restrict by"; }

                            string textRestrictField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.TextRestrictField1))
                                {
                                    if (aOutcomeDetailsDTO.TextRestrictField1.Length == 10)
                                    {
                                        textRestrictField1 = aOutcomeDetailsDTO.TextRestrictField1.ToString();
                                    }
                                    else
                                    {
                                        textRestrictField1 = Convert.ToInt32(aOutcomeDetailsDTO.TextRestrictField1).ToString();

                                        if (textRestrictField1 == "1") { textRestrictField1 = "Admission Date"; }
                                        else if (textRestrictField1 == "2") { textRestrictField1 = "Discharge Date"; }
                                        else if (textRestrictField1 == "3") { textRestrictField1 = "Visiting Date"; }
                                        else if (textRestrictField1 == "4") { textRestrictField1 = "Date of Birth"; }
                                        else if (textRestrictField1 == "5") { textRestrictField1 = "Date of Death"; }
                                        else if (textRestrictField1 == "6") { textRestrictField1 = "Parameter"; }
                                        else { textRestrictField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string textRestrictField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.TextRestrictField2))
                                {
                                    if (aOutcomeDetailsDTO.TextRestrictField2.Length == 10)
                                    {
                                        textRestrictField2 = aOutcomeDetailsDTO.TextRestrictField2.ToString();
                                    }
                                    else
                                    {
                                        textRestrictField2 = Convert.ToInt32(aOutcomeDetailsDTO.TextRestrictField2).ToString();

                                        if (textRestrictField2 == "1") { textRestrictField2 = "Admission Date"; }
                                        else if (textRestrictField2 == "2") { textRestrictField2 = "Discharge Date"; }
                                        else if (textRestrictField2 == "3") { textRestrictField2 = "Visiting Date"; }
                                        else if (textRestrictField2 == "4") { textRestrictField2 = "Date of Birth"; }
                                        else if (textRestrictField2 == "5") { textRestrictField2 = "Date of Death"; }
                                        else if (textRestrictField2 == "6") { textRestrictField2 = "Parameter"; }
                                        else { textRestrictField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(textRestrictField1) || !string.IsNullOrEmpty(textRestrictField2))
                            {
                                if (!string.IsNullOrEmpty(textRestrictField1))
                                {
                                    textRestrictField2 = "<br />" + textRestrictField2;
                                }

                                textRestrictString += "<tr>";
                                textRestrictString += "<th>Text Restrict</th>";
                                textRestrictString += "<td>" + textRestrictType + "</td>";
                                textRestrictString += "<td>" + textRestrictField1 + textRestrictField2 + "</td>";
                                textRestrictString += "</tr>";
                            }


                            // Building radiologyString --------------------------------------------------------------------

                            string radiologyString = string.Empty;
                            string radiologyType = string.Empty;
                            if (aOutcomeDetailsDTO.RadiologyType == 1) { radiologyType = "MRI"; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 2) { radiologyType = "CT"; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 3) { radiologyType = "X-Ray"; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 4) { radiologyType = "USG"; }

                            int radiologyListType = 0;
                            if (aOutcomeDetailsDTO.RadiologyType == 1) { radiologyListType = 7; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 2) { radiologyListType = 8; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 3) { radiologyListType = 9; }
                            else if (aOutcomeDetailsDTO.RadiologyType == 4) { radiologyListType = 10; }

                            GenericListDTO radiologyList = aListBLL.GetGenericList(aOutcomeDetailsDTO.RadiologyId, radiologyListType);

                            string radiologyDesription = string.Empty;
                            if (radiologyList != null) { radiologyDesription = radiologyList.Description; }

                            if (!string.IsNullOrEmpty(radiologyDesription))
                            {
                                radiologyString += "<tr>";
                                radiologyString += "<th>Radiology</th>";
                                radiologyString += "<td>" + radiologyType + "</td>";
                                radiologyString += "<td>" + radiologyDesription + "</td>";
                                radiologyString += "</tr>";
                            }


                            // Building radiologyRestrictString ------------------------------------------------------------

                            string radiologyRestrictString = string.Empty;
                            string radiologyRestrictType = string.Empty;
                            if (aOutcomeDetailsDTO.RadiologyRestrictType == 1) { radiologyRestrictType = "Related To"; }
                            else if (aOutcomeDetailsDTO.RadiologyRestrictType == 2) { radiologyRestrictType = "Time Interval"; }
                            else if (aOutcomeDetailsDTO.RadiologyRestrictType == 3) { radiologyRestrictType = "Restrict by"; }

                            string radiologyRestrictField1 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.RadiologyRestrictField1))
                                {
                                    if (aOutcomeDetailsDTO.RadiologyRestrictField1.Length == 10)
                                    {
                                        radiologyRestrictField1 = aOutcomeDetailsDTO.RadiologyRestrictField1.ToString();
                                    }
                                    else
                                    {
                                        radiologyRestrictField1 = Convert.ToInt32(aOutcomeDetailsDTO.RadiologyRestrictField1).ToString();

                                        if (radiologyRestrictField1 == "1") { radiologyRestrictField1 = "Admission Date"; }
                                        else if (radiologyRestrictField1 == "2") { radiologyRestrictField1 = "Discharge Date"; }
                                        else if (radiologyRestrictField1 == "3") { radiologyRestrictField1 = "Visiting Date"; }
                                        else if (radiologyRestrictField1 == "4") { radiologyRestrictField1 = "Date of Birth"; }
                                        else if (radiologyRestrictField1 == "5") { radiologyRestrictField1 = "Date of Death"; }
                                        else if (radiologyRestrictField1 == "6") { radiologyRestrictField1 = "Parameter"; }
                                        else { radiologyRestrictField1 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            string radiologyRestrictField2 = string.Empty;
                            try
                            {
                                if (!string.IsNullOrEmpty(aOutcomeDetailsDTO.RadiologyRestrictField2))
                                {
                                    if (aOutcomeDetailsDTO.RadiologyRestrictField2.Length == 10)
                                    {
                                        radiologyRestrictField2 = aOutcomeDetailsDTO.RadiologyRestrictField2.ToString();
                                    }
                                    else
                                    {
                                        radiologyRestrictField2 = Convert.ToInt32(aOutcomeDetailsDTO.RadiologyRestrictField2).ToString();

                                        if (radiologyRestrictField2 == "1") { radiologyRestrictField2 = "Admission Date"; }
                                        else if (radiologyRestrictField2 == "2") { radiologyRestrictField2 = "Discharge Date"; }
                                        else if (radiologyRestrictField2 == "3") { radiologyRestrictField2 = "Visiting Date"; }
                                        else if (radiologyRestrictField2 == "4") { radiologyRestrictField2 = "Date of Birth"; }
                                        else if (radiologyRestrictField2 == "5") { radiologyRestrictField2 = "Date of Death"; }
                                        else if (radiologyRestrictField2 == "6") { radiologyRestrictField2 = "Parameter"; }
                                        else { radiologyRestrictField2 = string.Empty; }
                                    }
                                }
                            }
                            catch { }

                            if (!string.IsNullOrEmpty(radiologyRestrictField1) || !string.IsNullOrEmpty(radiologyRestrictField2))
                            {
                                if (!string.IsNullOrEmpty(radiologyRestrictField1))
                                {
                                    radiologyRestrictField2 = "<br />" + radiologyRestrictField2;
                                }

                                radiologyRestrictString += "<tr>";
                                radiologyRestrictString += "<th>Radiology Restrict</th>";
                                radiologyRestrictString += "<td>" + radiologyRestrictType + "</td>";
                                radiologyRestrictString += "<td>" + radiologyRestrictField1 + radiologyRestrictField2 + "</td>";
                                radiologyRestrictString += "</tr>";
                            }


                            string outcomeDetailsText = string.Empty;

                            if (aOutcomeDetailsDTO != null)
                            {
                                outcomeDetailsText += "<div class='outcomedetails-popup'>";
                                outcomeDetailsText += "<table class='table table-condensed table-striped'>";
                                outcomeDetailsText += codeString;
                                outcomeDetailsText += codeRestrictString;
                                outcomeDetailsText += dateString;
                                outcomeDetailsText += readingsString;
                                outcomeDetailsText += readingsRestrictString;
                                outcomeDetailsText += textString;
                                outcomeDetailsText += textRestrictString;
                                outcomeDetailsText += radiologyString;
                                outcomeDetailsText += radiologyRestrictString;
                                outcomeDetailsText += "</table>";
                                outcomeDetailsText += "</div>";
                            }

                            outcomeText += "<div>" + listOfOutomes[j].FieldName + ": " + listOfOutomes[j].FieldValue + outcomeDetailsText + "</div>";
                        }

                        if (!string.IsNullOrEmpty(outcomeText))
                        {
                            lstBoxParameter.Items[i].Attributes.Add("title", lstBoxParameter.Items[i].Text);
                            lstBoxParameter.Items[i].Attributes.Add("data-content", outcomeText);
                        }
                    }
                }
            }
            catch { }

            if (lstBoxInclusion.Items.Count > 0)
            {
                DeleteInclusionLinkButton.Visible = true;
                EditInclusionLinkButton.Visible = true;
                ShareInclusionLinkButton.Visible = true;
            }
            else
            {
                DeleteInclusionLinkButton.Visible = false;
                EditInclusionLinkButton.Visible = false;
                ShareInclusionLinkButton.Visible = false;
            }

            if (lstBoxParameter.Items.Count > 0)
            {
                DeleteParameterLinkButton.Visible = true;
                EditParameterLinkButton.Visible = true;
                ShareParameterLinkButton.Visible = true;
            }
            else
            {
                DeleteParameterLinkButton.Visible = false;
                EditParameterLinkButton.Visible = false;
                ShareParameterLinkButton.Visible = false;
            }
        }

        #endregion


        protected void RedirectUser(UserDTO userDTO)
        {
            if (userDTO != null)
            {
                if (userDTO.UserType == "Administrator")
                {
                    Response.Redirect("AdministratorDashboard.aspx");
                }
            }
            else
            {
                Response.Redirect("Index.aspx");
            }
        }
    }
}